package org.example.redisspring;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Set;


//后续redis测试的各种方法都通过这个controller类来触发http请求
@RestController
public class MyController {


    @Autowired
    StringRedisTemplate redisTemplate;


    @ResponseBody
    @GetMapping("/testString")
    public String testString() {
        redisTemplate.opsForValue().set("key", "111");
        redisTemplate.opsForValue().set("key2", "222");
        redisTemplate.opsForValue().set("key3", "333");
        String key = redisTemplate.opsForValue().get("key");
        System.out.println(key);
        return "OK";
    }

    @ResponseBody
    @GetMapping("/testList")
    public String testList() {
        //先清空redis库
        //execute里面的回调函数要return一个值 这个值就是execute 的返回值
        //execute相当于Spring提供的后手 一旦redisTemplate里面没有提供相应的方法 如flushall flushaDB 之类的 我们可以使用execute回调函数来执行到redis
        //相关的一些原生命令
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushDb();
            return null;
        });
        redisTemplate.opsForList().leftPush("key", "111");
        redisTemplate.opsForList().leftPush("key2", "222");
        redisTemplate.opsForList().leftPush("key3", "333");

        System.out.println(redisTemplate.opsForList().rightPop("key"));
        System.out.println(redisTemplate.opsForList().rightPop("key2"));
        System.out.println(redisTemplate.opsForList().rightPop("key3"));
        return "OK";
    }

    @ResponseBody
    @GetMapping("/testSet")
    public String testSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushDb();
            return null;
        });
        redisTemplate.opsForSet().add("key", "111", "222", "333");
        Set<String> members = redisTemplate.opsForSet().members("key");
        System.out.println("members: " + members);
        Boolean exists = redisTemplate.opsForSet().isMember("key", "111");
        System.out.println("exists: " + exists);
        Long count = redisTemplate.opsForSet().size("key");
        System.out.println("count: " + count);
        Long delete = redisTemplate.opsForSet().remove("key", "111", "222");
        System.out.println("delete: " + delete);
        return "Ok";
    }

    @ResponseBody
    @GetMapping("/testHash")
    public String testHash() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushDb();
            return null;
        });
        HashMap<String, String> map = new HashMap<>();
        map.put("f1", "111");
        map.put("f2", "222");
        map.put("f3", "333");
        redisTemplate.opsForHash().putAll("key", map);
        String value = (String) redisTemplate.opsForHash().get("key", "f1");
        System.out.println("value: " + value);
        Boolean exists = redisTemplate.opsForHash().hasKey("key", "f1");
        System.out.println("exists: " + exists);
        Long delete = redisTemplate.opsForHash().delete("key", "f1", "f2");
        System.out.println("delete: " + delete);//返回的是删除的键值对个数
        Long len = redisTemplate.opsForHash().size("key");//返回当前value 的哈希表中的键值对个个数
        System.out.println("len:" + len);
        return "OK";
    }


    @ResponseBody
    @GetMapping("/testZSet")
    public String testZSet() {
        redisTemplate.execute((RedisConnection connection) -> {
            connection.flushAll();
            return null;
        });
        redisTemplate.opsForZSet().add("key", "zhangsan", 10);
        redisTemplate.opsForZSet().add("key", "lisi", 20);
        redisTemplate.opsForZSet().add("key", "wangwu", 30);
        Set<String> members = redisTemplate.opsForZSet().range("key", 0, -1);
        System.out.println("members: " + members);
        Set<ZSetOperations.TypedTuple<String>> membersWithScore = redisTemplate.opsForZSet().rangeWithScores("key", 0, -1);
        System.out.println("membersWithScore: " + membersWithScore);
        Double score = redisTemplate.opsForZSet().score("key", "zhangsan");
        System.out.println("score: " + score);
        Long remove = redisTemplate.opsForZSet().remove("key", "zhangsan");
        System.out.println("remove: " + remove);
        Long size = redisTemplate.opsForZSet().size("key");
        System.out.println("long: " + size);
        Long rank = redisTemplate.opsForZSet().rank("key", "lisi");
        System.out.println("rank: " + rank);
        return "OK";
    }

}
